package com.yuda.tools.checkanswer.controller;

import com.yuda.tools.checkanswer.dto.FormalAnswerMarked;
import com.yuda.tools.checkanswer.dto.SubjectIdAndPaperId;
import com.yuda.tools.checkanswer.service.CheckAnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class AnswerSupplement {

    @Autowired
    private CheckAnswerService checkAnswerService;

    //todo 待测试
    @GetMapping("/supplementAnswerFromMarkTable")
    public void supplementAnswerFromMarkTable(String studentId, String subjectRelationtId) {

        //先从marked 表中找到这个人 的数据
        List<Integer> list = checkAnswerService.listQuestionIdsFromMarked(studentId, subjectRelationtId);

        //找出这个科目所有的题目
        List<Integer> all = checkAnswerService.listAllQuestionsBySubjectId(subjectRelationtId);

        //去重
        all.removeAll(list);

        //拼装数据准备入库
        //根据subjectRelationId找 subjectId 和paperId
        SubjectIdAndPaperId subjectIdAndPaperId = checkAnswerService.findSubjectIdAndPaperId(subjectRelationtId);

        //查询此人的准考证号码
        String zkzNum = checkAnswerService.getZkzNumByStudentId(studentId);

        List<FormalAnswerMarked> data = new ArrayList<>();
        for (Integer quesId : all) {
            FormalAnswerMarked formalAnswerMarked = new FormalAnswerMarked();
            formalAnswerMarked.setAnswer("-1");
            formalAnswerMarked.setPaperId(subjectIdAndPaperId.getPaperId());
            formalAnswerMarked.setSubjectId(subjectIdAndPaperId.getSubjectId());
            formalAnswerMarked.setSubjectRelationId(subjectRelationtId);
            formalAnswerMarked.setQuesId(quesId + "");
            formalAnswerMarked.setStudentId(studentId);
            formalAnswerMarked.setZkzNum(zkzNum);
            data.add(formalAnswerMarked);
        }

        //插入到marked表
        checkAnswerService.supplementAnswerToMarkedTable(data);

        //更新type字段 type题目类型
        checkAnswerService.updateTypeForMarkedTable();
    }

}
